package com.gopay.common.cps.manager.gp.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.cps.dao.gp.CpsGpContractInfoDAO;
import com.gopay.common.cps.dao.gp.CpsGpContractLogDAO;
import com.gopay.common.cps.manager.gp.CpsGpContractInfoManager;
import com.gopay.common.domain.cps.gp.CpsGpContractInfo;
import com.gopay.common.domain.cps.gp.CpsGpContractLog;

/**
 * P2P签约协议信息
 * 
 * @CpsGpContractInfoManagerImpl.java
 * @author lizheng
 * @2014-4-18 下午3:29:44 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("cpsGpContractInfoManager")
public class CpsGpContractInfoManagerImpl implements CpsGpContractInfoManager {
    @Resource(name = "cpsGpContractInfoDAO")
    private CpsGpContractInfoDAO cpsGpContractInfoDAO;
    @Resource(name="cpsGpContractLogDAO")
    private CpsGpContractLogDAO cpsGpContractLogDAO;

    @Override
    public CpsGpContractInfo get(Serializable id) {
        return cpsGpContractInfoDAO.find(CpsGpContractInfo.class, id);
    }

    @Override
    public List<CpsGpContractInfo> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<CpsGpContractInfo> getAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getNextVal() {
        return cpsGpContractInfoDAO.getNextVal();
    }

    
    @Override
    public CpsGpContractInfo getByContractNo(String contractNo) {

        return cpsGpContractInfoDAO.getByContractNo(contractNo);
    }

    @Override
    public boolean isSign(String trustAcct, String excluCustId) {
        return cpsGpContractInfoDAO.isSign(trustAcct, excluCustId);
    }

    
    @Override
    public CpsGpContractInfo getByExcluAcct(String excluAcctId){
        return this.cpsGpContractInfoDAO.getByExcluAcctId(excluAcctId);
    }

    @Override
    public void update(CpsGpContractInfo info) {
        cpsGpContractInfoDAO.update(info);
        
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public void saveAutoTransferByExcluCustId(Map<String, String> map) {
        StringBuffer upSql = new StringBuffer();
        upSql.append("   UPDATE CPS_P2P_CONTRACT_INFO CI ")
             .append("      SET CI.PER_LIMIT   =")
             .append(map.get("perLimitAmt"))
             .append(", ")
             .append("          CI.DAY_LIMIT   =")
             .append(map.get("dayLimitAmt"))
             .append(", ")
             .append("          CI.AUTO_TRANSFER = 1, ")
             .append("          CI.LAST_UPD_TS = TO_DATE('")
             .append(map.get("time"))
             .append("', 'yyyy-MM-dd hh24:mi:ss') ")
             .append("    WHERE CI.EXCLU_CUST_ID = '")
             .append(map.get("excluCustId"))
             .append("'");
        cpsGpContractInfoDAO.updateBySql(upSql.toString(), null);
    }

    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public void saveGpContractInfoAndLog(CpsGpContractInfo info,CpsGpContractLog log) {
        cpsGpContractInfoDAO.save(info);
        cpsGpContractLogDAO.save(log);
    }

}
